/*张欢欢  驾驶员信息js*/
var drivertInfos = [];
var deleteFlag;
var index;

$(document).ready(function() {
	showLoading();
	$('title', parent.document).html($('title').text());
	setBaseUrl();
	setTimeout(hideLoading, 500);
});

function findFirstPage(){
	$("#page").val(1);
	$("#currentPage").val(1);
	search();
}

//驾驶员信息查询
function search(){
	showLoading();
	seach = 1;
	$("#chkAll").attr("checked", false);
	$.ajax({
		type : 'post',
		data : $("#searchDriverInfo").serialize(),
		url : 'searchDriverInfo',
		dataType : 'json',
		success : function(data){
			setTimeout(hideLoading, 500);
			clearTable("driverInfoTbody");
			var page = $('#page').val();
			var rows = $('#rows').val();
			totalPage = data && data.totalPage || 0;
			totalCnt = data && data.totalCnt || 0;
			drivertInfos = data && data.driverInfoList || [];
			document.getElementById("total").innerText ='共'+totalCnt+'条/'+'共'+totalPage+'页';
			// 当前页数
			var currentPage = $('#currentPage').val();
			// 每页显示的条数
			var rows = $('#rows').val();
			// 画面表格的body
			var tbody = $("#driverInfoTbody");
			var length = drivertInfos.length;
			if (length == 0) {
				showMessge("没有查询结果");
			} else {
				for(var i=0; i<length; i++){
					// 序号
					var rowNo = (currentPage - 1) * rows + i + 1;
					// 追加显示数据
					var $tr = $('<tr>').appendTo(tbody);
					$("<td>").html('<input name="chk" id="chk" type="checkbox">').appendTo($tr);
					$("<td>").html(rowNo).appendTo($tr);
					$("<td>").html(drivertInfos[i].cardNo).appendTo($tr);
					$("<td>").html(drivertInfos[i].driverName).appendTo($tr);
					$("<td>").html(drivertInfos[i].telephoneNo).appendTo($tr);
					$("<td>").html(drivertInfos[i].email).appendTo($tr);
					$("<td>").html(drivertInfos[i].weChat).appendTo($tr);
					if(drivertInfos[i].sex == "1"){
						$("<td>").html("男").appendTo($tr);
					} else if(drivertInfos[i].sex == "0") {
	                    $("<td>").html("女").appendTo($tr);
	                } else {
	                	$("<td>").html("-").appendTo($tr);
	                }
					if(drivertInfos[i].enabled == true) {
	                    $("<td>").html("是").appendTo($tr);
	                } else {
	                    $("<td>").html("否").appendTo($tr);
	                }
				}
			}
		},
		error: function(data){
			setTimeout(hideLoading, 500);
			if (data.status == "403" && data.statusText == "Forbidden") {
				showMessge("没有访问权限" );
			} else {
				showMessge("查询失败!" );
			}
		},
		timeout: function(data){
			setTimeout(hideLoading, 500);
			showMessge("操作超时！");
		}
	})	
}
//添加
function save() {
	formReset("saveForm");
	$('#saveTitle').html('');
	var saveTitle = $('#saveTitle');
	var strong = document.createElement("strong");
	strong.innerHTML = '驾驶员信息添加';
	$("#saveCardNo").attr("disabled",false);
	$("#save").modal("show");
    deleteFlag = "save";
    saveTitle.append(strong);
}
function update() {
	var checkbox = getCheckbox("chk");
	var count = checkbox && checkbox.count;
	index = checkbox && checkbox.row_index;
	$('#saveTitle').html('');
	var saveTitle = $('#saveTitle');
	var strong = document.createElement("strong");
	strong.innerHTML = '驾驶员信息修改';
	if (count == 1) {
		$("#save").modal("show");
		// 设置弹出窗口的位置
		$("#saveCardNo").val(drivertInfos[index].cardNo).attr("disabled",true);
		$("#saveDriverName").val(drivertInfos[index].driverName);
		$("#saveTelephoneNo").val(drivertInfos[index].telephoneNo);
		$("#saveEmail").val(drivertInfos[index].email);
		$("#saveWeChat").val(drivertInfos[index].weChat);
		$("#saveSex").val(drivertInfos[index].sex);
		var enabled = drivertInfos[index].enabled;
		if (enabled){
			$("input[name=saveEnabled]:eq(1)").prop("checked", true);
		} else {
			$("input[name=saveEnabled]:eq(0)").prop("checked", true);
		}
	} else if (count == 0) {
		showMessge("请选择要操作的数据!");
	} else {
		showMessge("抱歉,不能选择多行!");
	}
	deleteFlag = "update";
	//初始化是否为空
	saveTitle.append(strong);
}
function confirm() {;
	var saveCardNo=$("#saveCardNo").val();
	var saveDriverName=$("#saveDriverName").val()
	var saveTelephoneNo=$("#saveTelephoneNo").val();
	var saveEmail=$("#saveEmail").val();
	var saveWeChat=$("#saveWeChat").val();
	var saveSex = $("#saveSex").val();
	var enabled = $("input[name=saveEnabled]:checked").val();
	if (!saveCardNo) {
		showMessge("员工卡号不能为空");
		return;
	}
	if (!saveDriverName) {
		showMessge("驾驶员名称不能为空");
		return;
	}
	if (!saveTelephoneNo) {
		showMessge("手机号不能为空");
		return;
	}
	if (!saveEmail) {
		showMessge("邮箱不能为空");
		return;
	}
	if (!saveWeChat) {
		showMessge("微信号不能为空");
		return;
	}
	if (!saveSex) {
		showMessge("性别未选择");
		return;
	}
    if (deleteFlag == "save") {
    	showLoading();
		  $.ajax({
		    type: "POST",
		    url: 'saveDriverInfo',
		   	data: {
		   		CardNo : saveCardNo,
		   		DriverName : saveDriverName,
		   		TelephoneNo : saveTelephoneNo,
		   		Email : saveEmail,
		   		WeChat : saveWeChat,
		   		Sex : saveSex,
		   		Enabled : enabled
		   	},
			dataType:'json',
			success: function(data){
				setTimeout(hideLoading, 500);
				$('#save').modal('hide');
				var result = data && data.result;
				if (result == "Success") {
					showMessge("添加成功");
				} else if (result == "NotFound"){
					showMessge("添加失败");
				} else if (result == "CardNoNoNull") {
					showMessge("员工卡号已存在,请重新输入");
				} else if (result == "DriverNameNoNull") {
					showMessge("驾驶员姓名已存在,请重新输入");
				} else if (result == "TelephoneNoNoNull") {
					showMessge("手机号已存在,请重新输入");
				} else if (result == "EmailNoNull") {
					showMessge("邮箱已存在,请重新输入");
				} else if (result == "WeChatNoNull") {
					showMessge("微信号已存在,请重新输入");
				} else {
					showMessge("添加异常");
				}
				search();
				formReset("saveForm");
			},
			error : function(data) {
				setTimeout(hideLoading, 500);
				if (data.status == "403" && data.statusText == "Forbidden") {
					showMessge("您没有操作权限!" );
				} else {
					showMessge("操作失败!" );
				}
			},
			timeout: function(data){
				setTimeout(hideLoading, 500);
				showMessge("操作超时！");
			}
	    });
   } else if (deleteFlag == "update") {
	   showLoading();
	   var saveCardNo = drivertInfos[index].cardNo;
	   $.ajax({
			url : 'updateDriverInfo',
			type : 'post',
			data : {
				CardNo : saveCardNo,
				DriverName : saveDriverName,
				TelephoneNo :saveTelephoneNo,
				Email : saveEmail,
				WeChat : saveWeChat,
		   		Sex : saveSex,
				Enabled : enabled
			},
			dataType : 'json',
			success : function(data){
				setTimeout(hideLoading, 500);
				$('#save').modal('hide');
				var result = data && data.result;
				if (result == "Success") {
					showMessge("修改成功");
				} else if (result == "NotFound"){
					showMessge("修改失败");
				} else if (result == "DriverNameNoNull") {
					showMessge("驾驶员姓名已存在,请重新输入");
				} else if (result == "TelephoneNoNoNull") {
					showMessge("手机号已存在,请重新输入");
				} else if (result == "EmailNoNull") {
					showMessge("邮箱已存在,请重新输入");
				} else if (result == "WeChatNoNull") {
					showMessge("微信号已存在,请重新输入");
				} else if (result == "CardNoNoNull") {
					showMessge("员工卡号已存在,请重新输入");
				} else {
					showMessge("修改异常");
				}
				search();
				formReset("saveForm");
			},
			error : function(data) {
				setTimeout(hideLoading, 500);
				if (data.status == "403" && data.statusText == "Forbidden") {
					showMessge("您没有操作权限!" );
				} else {
					showMessge("操作失败!" );
				}
			},		
			timeout: function(data){
				setTimeout(hideLoading, 500);
				showMessge("操作超时！");
			}
		});
   }
}
//删除
function sure(){
	showLoading();
	var checkbox = getCheckbox("chk");
	var count = checkbox && checkbox.count || 0;
	var index = undefined;
	var cardNo = undefined;
	var cardNoList = [];
	for (var i = 0; i<count; i++) {
		index = checkbox && checkbox.row_index[i] || 0;
		cardNo = drivertInfos && drivertInfos[index] && drivertInfos[index].cardNo;
		cardNoList.push(cardNo);
	}
	$.ajax({
		type : "post",
		url : "deleteDriverInfo",
		dataType : "json",
		data : {
			cardNo: cardNoList.toString()
		},
		success : function(data) {
			setTimeout(hideLoading, 500);
			var result = data && data.result;
			if (result == "Success") {
				showMessge("删除成功");
			} else if (result == "NotFound") {
				showMessge("删除数据不存在");
			} else {
				showMessge("删除失败");
			}
			search();
		},
		error : function(data) {
			setTimeout(hideLoading, 500);
			if (data.status == "403" && data.statusText == "Forbidden") {
				showMessge("没有访问权限!" );
			} else {
				showMessge("操作失败!" );
			}
		},
		timeout: function(data){
			setTimeout(hideLoading, 500);
			showMessge("操作超时！");
		}
	});
}